package a_doubleWrite;

/**
 * 209. 长度最小的子数组
 *
 * @author luolidong
 * @version 1.0
 * @create 2021-07-30 15:25
 */
public class L209 {
    public int minSubArrayLen(int target, int[] nums) {
        if (nums.length == 0) return 0;
        int min = Integer.MAX_VALUE;
        for (int i = 0; i < nums.length; i++) {
            int sum = 0;
            for (int j = i + 1; j < nums.length; j++) {
                sum += nums[j];
                if (sum >= target) {
                    min = Math.min(min, j - i + 1);
                    break;
                }
            }
        }
        return min;
    }

    public int minSubArrayLen1(int target, int[] nums) {
        if (nums.length == 0) return 0;
        int min = Integer.MAX_VALUE;
        int sum = 0;
        int start = 0;
        for (int i = 0; i < nums.length; i++) {
            sum += nums[i];
            //注意这里要循环判断
            while (sum >= target) {
                min = Math.min(min, i - start + 1);
                //左边界向右滑动
                sum -= nums[start];
                start++;
            }
        }
        return min==Integer.MAX_VALUE? 0:min;
    }
}
